#!/bin/sh
#
# install -- Install docker engine
#
# ref: https://docs.docker.com/engine/installation/linux/ubuntulinux/
#
# ref: https://jpetazzo.github.io/2014/01/29/docker-device-mapper-resize/
#

which docker > /dev/null 2>&1 && exit 0

[ -f /etc/issue ] && grep -qi ubuntu /etc/issue
if [ $? -eq 0 ]; then
    echo "LOG: Install docker-engine with $0"
else
    echo "LOG: Please install docker-engine with doc: https://docs.docker.com/engine/installation/linux/ubuntulinux/"
    exit 1
fi

# Install docker-engine for Ubuntu

[ `id -u` -ne 0 ] && SUDO=sudo

$SUDO apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
version=`sed -n -e "/ main/p" /etc/apt/sources.list | grep -v ^# | head -1 | cut -d' ' -f3`
$SUDO sh -c 'echo "deb http://mirrors.aliyun.com/docker-engine/apt/repo/ ubuntu-'${version}' main" > /etc/apt/sources.list.d/docker.list'
$SUDO apt-get -y update
$SUDO apt-get -y install apt-transport-https ca-certificates
$SUDO apt-get -y install bridge-utils
$SUDO apt-get -y --force-yes install docker-engine
$SUDO usermod -aG docker $USER

# The --bip works like https://gist.github.com/ismell/6689836
$SUDO sed -i -e "/DOCKER HACK START 578327498237/,/DOCKER HACK END 789527394722/d" /etc/default/docker
$SUDO bash -c 'cat <<EOF >> /etc/default/docker
# CLOUD LAB: DOCKER HACK START 578327498237
DOCKER_OPTS="\$DOCKER_OPTS --insecure-registry registry.mirrors.aliyuncs.com"
#DOCKER_OPTS="\$DOCKER_OPTS --registry-mirror https://xxx.mirror.aliyuncs.com"
# DOCKER_OPTS="\$DOCKER_OPTS --registry-mirror=https://docker.mirrors.ustc.edu.cn"
# DOCKER_OPTS="\$DOCKER_OPTS --dns 119.29.29.29 --dns 180.76.76.76"
# DOCKER_OPTS="\$DOCKER_OPTS --dns 223.5.5.5 --dns 114.114.114.114"
# DOCKER_OPTS="\$DOCKER_OPTS --dns 8.8.8.8 --dns 8.8.4.4"
DOCKER_OPTS="\$DOCKER_OPTS --bip=10.66.0.10/16"
DOCKER_OPTS="\$DOCKER_OPTS --storage-opt dm.basesize=5G"
# CLOUD LAB: DOCKER HACK END 789527394722
EOF'
# Restart to make sure the above opts work
$SUDO ifconfig docker0 down
$SUDO brctl delbr docker0
$SUDO service docker restart
